/*
 * @Author: error: git config user.name & please set dead value or install git
 * @Date: 2022-08-31 10:26:21
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2023-06-30 10:42:42
 * @FilePath: \stm32g431_AllRings\applications\srt_im.h
 * @Description:
 *
 * Copyright (c) 2022 by error: git config user.name && git config user.email & please set dead value or install git, All Rights Reserved.
 */
#ifndef __SRT_IM_H
#define __SRT_IM_H

#include <rtthread.h>
#include <board.h>
#include "rtdebug.h"
#include "allrings_sys.h"
// #include "my_fdcan_drv.h"

/**************************SRT-IM 气动手寄存器表***********************/
// 线圈寄存器01
#define REG_CLEAR_CTP 0x0000        // （清空工作次数） bool
#define REG_IAP_UPDATA 0x0001       // （IAP 标志位） bool
#define REG_CLEAR_ERR 0x0002        // （重置弱报警标志） bool
#define REG_CALIBRATION_ZERO 0x0003 // （零点校准） bool

// 读离散输入02
#define REG_P_IN_BIT 0x0000    // （正压触发 IO） bool
#define REG_N_IN_BIT 0x0001    // （负压触发 IO） bool
#define REG_KEYP_IN_BIT 0x0002 // （正压按键） bool
#define REG_KEYN_IN_BIT 0x0003 // （负压按键） bool

// 输入寄存器
#define REG_RTP 0x0000            // （实时气压） float
#define REG_CTP 0x0002            // （工作次数） uint32_t
#define REG_CTP_SAVE 0x0004       // （总工作次数） uint32_t
#define REG_POS_CONFIG 0x0006     // （正压设定值） int16_t
#define REG_NEG_CONFIG 0x0007     // （负压设定值） int16_t
#define REG_NOW_TRG 0x0008        // （当前触发状态） uint16_t
#define REG_END_TRG 0x0009        // （完成触发状态） uint16_t
#define REG_ERR_BIT 0x000A        // （报警信息） uint16_t
#define REG_HARD_VER 0x000B       // （硬件版本信息） uint32_t
#define REG_SOFTWARE_VER 0x000D   // （软件版本） char[24]
#define REG_TRG_GEAR 0x0019       // （当前模式） uint16_t
#define REG_POS_ADJUST_MAX 0x0020 // （正压调节的最大值） int16_t
#define REG_NEG_ADJUST_MAX 0x0021 // （负压调节的最大值） int16_t

// 保持寄存器
#define RTU_ID 0x0000    // （设备 ID） uint16_t
#define TRG 0x0001       // （触发状态） uint16_t
#define CYC_TIME 0x0002  // （循环间隔） uint32_t
#define TS_TIME 0x0004   // （通信触发时间） uint32_t
#define POS_SET 0x0006   // （正压设定值） int16_t
                         //     0x0007~0x000D 保留
#define NEG_SET 0x000E   // （负压设定值） int16_t
                         // 0x000F~0x0015 保留
#define GEAR 0x0016      // （工作模式） uint16_t
                         // 0x0017~0x0019 保留
#define IAP_PASS 0x001A  // （IAP 升级秘钥） uint32_t
#define CAIL_ZERO 0x001C // （气压传感器零点值） uint32

/*********************************************************************/
#define SRT_CMD_LIST_TAB                              \
    X_MACRO(_SRT_IM_R_COILS_CLEAR_CTR, 0X10)          \
    X_MACRO(_SRT_IM_R_COILS_CLEAR_ERR, 0X11)          \
    X_MACRO(_SRT_IM_R_COILS_CALIBRATION_ZERO, 0X12)   \
    /*02*/                                            \
    X_MACRO(_SRT_IM_R_DISINPUTSREG_P_IN_BIT, 0X20)    \
    X_MACRO(_SRT_IM_R_DISINPUTSREG_N_IN_BIT, 0X21)    \
    X_MACRO(_SRT_IM_R_DISINPUTSREG_KEYP_IN_BIT, 0X22) \
    X_MACRO(_SRT_IM_R_DISINPUTSREG_KEYN_IN_BIT, 0X23) \
    /*04-输入寄存器(可读不可写)*/           \
    X_MACRO(_SRT_IM_R_INPUTREG_RTP, 0X40)             \
    X_MACRO(_SRT_IM_R_INPUTREG_CTP, 0X41)             \
    X_MACRO(_SRT_IM_R_INPUTREG_CTP_SAVE, 0X42)        \
    X_MACRO(_SRT_IM_R_INPUTREG_POS_CONFIG, 0X43)      \
    X_MACRO(_SRT_IM_R_INPUTREG_NEG_CONFIG, 0X44)      \
    X_MACRO(_SRT_IM_R_INPUTREG_NOW_TRG, 0X45)         \
    X_MACRO(_SRT_IM_R_INPUTREG_END_TRG, 0X46)         \
    X_MACRO(_SRT_IM_R_INPUTREG_ERR_BIT, 0X47)         \
    X_MACRO(_SRT_IM_R_INPUTREG_HARD_VER, 0X48)        \
    X_MACRO(_SRT_IM_R_INPUTREG_SOFTWARE_VER, 0X49)    \
    X_MACRO(_SRT_IM_R_INPUTREG_TRG_GEAR, 0X50)        \
    X_MACRO(_SRT_IM_R_INPUTREG_POS_ADJUST_MAX, 0X51)  \
    X_MACRO(_SRT_IM_R_INPUTREG_NEG_ADJUST_MAX, 0X52)  \
    /*03*/                                            \
    X_MACRO(_SRT_IM_R_KEEPREG_RTU_ID, 0X60)           \
    X_MACRO(_SRT_IM_R_KEEPREG_TRG, 0X06 /*0X61*/)     \
    X_MACRO(_SRT_IM_R_KEEPREG_CYC_TIME, 0X62)         \
    X_MACRO(_SRT_IM_R_KEEPREG_TS_TIME, 0X63)          \
    X_MACRO(_SRT_IM_R_KEEPREG_POS_SET, 0X64)          \
    X_MACRO(_SRT_IM_R_KEEPREG_NEG_SET, 0X65)          \
    X_MACRO(_SRT_IM_R_KEEPREG_GEAR, 0X66)             \
    X_MACRO(_SRT_IM_R_KEEPREG_IAP_PASS, 0X67)         \
    X_MACRO(_SRT_IM_R_KEEPREG_CAIL_ZERO, 0X68)        \
    /*06/10-写保持寄存器*/                      \
    X_MACRO(_SRT_IM_W_KEEPREG_RTU_ID, 0X80)           \
    X_MACRO(_SRT_IM_W_KEEPREG_TRG, 0x0A /*0X81*/)     \
    X_MACRO(_SRT_IM_W_KEEPREG_CYC_TIME, 0X82)         \
    X_MACRO(_SRT_IM_W_KEEPREG_TS_TIME, 0X83)          \
    X_MACRO(_SRT_IM_W_KEEPREG_POS_SET, 0X84)          \
    X_MACRO(_SRT_IM_W_KEEPREG_NEG_SET, 0X85)          \
    X_MACRO(_SRT_IM_W_KEEPREG_GEAR, 0X86)             \
    X_MACRO(_SRT_IM_W_KEEPREG_IAP_PASS, 0X87)         \
    X_MACRO(_SRT_IM_W_KEEPREG_CAIL_ZERO, 0X88)

typedef enum
{
#define X_MACRO(a, b) a = b,
    SRT_CMD_LIST_TAB
#undef X_MACRO
} srt_cmd_tab;

///*这几个专门用于判断SRT通讯异常的报错*/
//#define DEVICE_RS485_ERR_THRESHOLD 3 // 错误触发阈值
//#define DEVICE_RS485_ERR_TIMEOUT 0XFE
//#define DEVICE_RS485_ERR_FAILURE 0XFF

// typedef enum
// {
//     /****读操作***/
//     /*01*/
//     _SRT_IM_R_COILS_CLEAR_CTR = 0X10, // 清空工作次数
//     _SRT_IM_R_COILS_CLEAR_ERR,
//     _SRT_IM_R_COILS_CALIBRATION_ZERO,

//     /*02*/
//     _SRT_IM_R_DISINPUTSREG_P_IN_BIT = 0X20,
//     _SRT_IM_R_DISINPUTSREG_N_IN_BIT,
//     _SRT_IM_R_DISINPUTSREG_KEYP_IN_BIT,
//     _SRT_IM_R_DISINPUTSREG_KEYN_IN_BIT,

//     /*04-输入寄存器(可读不可写)*/
//     _SRT_IM_R_INPUTREG_RTP = 0X40,
//     _SRT_IM_R_INPUTREG_CTP,
//     _SRT_IM_R_INPUTREG_CTP_SAVE,
//     _SRT_IM_R_INPUTREG_POS_CONFIG,
//     _SRT_IM_R_INPUTREG_NEG_CONFIG,
//     _SRT_IM_R_INPUTREG_NOW_TRG,
//     _SRT_IM_R_INPUTREG_END_TRG,
//     _SRT_IM_R_INPUTREG_ERR_BIT,
//     _SRT_IM_R_INPUTREG_HARD_VER,
//     _SRT_IM_R_INPUTREG_SOFTWARE_VER,
//     _SRT_IM_R_INPUTREG_TRG_GEAR = 0X50,
//     _SRT_IM_R_INPUTREG_POS_ADJUST_MAX,
//     _SRT_IM_R_INPUTREG_NEG_ADJUST_MAX,

//     /*03*/
//     _SRT_IM_R_KEEPREG_RTU_ID = 0X60,
//     _SRT_IM_R_KEEPREG_TRG,
//     _SRT_IM_R_KEEPREG_CYC_TIME,
//     _SRT_IM_R_KEEPREG_TS_TIME,
//     _SRT_IM_R_KEEPREG_POS_SET,
//     _SRT_IM_R_KEEPREG_NEG_SET,
//     _SRT_IM_R_KEEPREG_GEAR,
//     _SRT_IM_R_KEEPREG_IAP_PASS,
//     _SRT_IM_R_KEEPREG_CAIL_ZERO,

//     /***写操作***/
//     /*05*/

//     /*06/10-写保持寄存器*/
//     _SRT_IM_W_KEEPREG_RTU_ID = 0X80,
//     _SRT_IM_W_KEEPREG_TRG,
//     _SRT_IM_W_KEEPREG_CYC_TIME,
//     _SRT_IM_W_KEEPREG_TS_TIME,
//     _SRT_IM_W_KEEPREG_POS_SET,
//     _SRT_IM_W_KEEPREG_NEG_SET,
//     _SRT_IM_W_KEEPREG_GEAR,
//     _SRT_IM_W_KEEPREG_IAP_PASS,
//     _SRT_IM_W_KEEPREG_CAIL_ZERO,

// } srt_cmd;
struct _COM_ERR
{
    uint8_t rs485_err_timeout_counter; // 超时次数计数
    uint8_t rs485_err_failure_counter; // 失败次数计数
};

struct _SRT_IM_VALUES
{
    uint8_t addr;
    struct _COM_ERR srt_com_err_judge;
};

struct _srt_ctr
{
    struct _SRT_IM_VALUES srt_im_values;
    struct _fdcan_rxdata fdcan_fdback;                      // CAN原始数据结构体
    uint8_t (*device_cmd_srtim[256])(struct _srt_ctr *srt); // SRT-IM设备命令接口
    uint8_t (*inputcmd_valid_check)(uint8_t cmdcheck);
    void (*can_sendback_data)(struct _fdcan_rxdata *fdcandata);
};

extern struct _srt_ctr srt_im;
uint8_t srt_cmd_check(uint8_t cmdcheck);
void fdcan_send2up_data(struct _fdcan_rxdata *fdcandata);
/*****************************指令接口函数*************************/
/*01*/
uint8_t SRT_IM_R_COILS_CLEAR_CTR(struct _srt_ctr *srt);
uint8_t SRT_IM_R_COILS_CLEAR_ERR(struct _srt_ctr *srt);
uint8_t SRT_IM_R_COILS_CALIBRATION_ZERO(struct _srt_ctr *srt);

/*02*/
uint8_t SRT_IM_R_DISINPUTSREG_P_IN_BIT(struct _srt_ctr *srt);
uint8_t SRT_IM_R_DISINPUTSREG_N_IN_BIT(struct _srt_ctr *srt);
uint8_t SRT_IM_R_DISINPUTSREG_KEYP_IN_BIT(struct _srt_ctr *srt);
uint8_t SRT_IM_R_DISINPUTSREG_KEYN_IN_BIT(struct _srt_ctr *srt);

/*03*/
uint8_t SRT_IM_R_KEEPREG_RTU_ID(struct _srt_ctr *srt);
uint8_t SRT_IM_R_KEEPREG_TRG(struct _srt_ctr *srt);
uint8_t SRT_IM_R_KEEPREG_CYC_TIME(struct _srt_ctr *srt);
uint8_t SRT_IM_R_KEEPREG_TS_TIME(struct _srt_ctr *srt);
uint8_t SRT_IM_R_KEEPREG_POS_SET(struct _srt_ctr *srt);
uint8_t SRT_IM_R_KEEPREG_NEG_SET(struct _srt_ctr *srt);
uint8_t SRT_IM_R_KEEPREG_GEAR(struct _srt_ctr *srt);
uint8_t SRT_IM_R_KEEPREG_IAP_PASS(struct _srt_ctr *srt);
uint8_t SRT_IM_R_KEEPREG_CAIL_ZERO(struct _srt_ctr *srt);

/*04*/
uint8_t SRT_IM_R_INPUTREG_RTP(struct _srt_ctr *srt);
uint8_t SRT_IM_R_INPUTREG_CTP(struct _srt_ctr *srt);
uint8_t SRT_IM_R_INPUTREG_CTP_SAVE(struct _srt_ctr *srt);
uint8_t SRT_IM_R_INPUTREG_POS_CONFIG(struct _srt_ctr *srt);
uint8_t SRT_IM_R_INPUTREG_NEG_CONFIG(struct _srt_ctr *srt);
uint8_t SRT_IM_R_INPUTREG_NOW_TRG(struct _srt_ctr *srt);
uint8_t SRT_IM_R_INPUTREG_END_TRG(struct _srt_ctr *srt);
uint8_t SRT_IM_R_INPUTREG_ERR_BIT(struct _srt_ctr *srt);
uint8_t SRT_IM_R_INPUTREG_HARD_VER(struct _srt_ctr *srt);
uint8_t SRT_IM_R_INPUTREG_SOFTWARE_VER(struct _srt_ctr *srt);
uint8_t SRT_IM_R_INPUTREG_TRG_GEAR(struct _srt_ctr *srt);
uint8_t SRT_IM_R_INPUTREG_POS_ADJUST_MAX(struct _srt_ctr *srt);
uint8_t SRT_IM_R_INPUTREG_NEG_ADJUST_MAX(struct _srt_ctr *srt);
/*06*/
uint8_t SRT_IM_W_KEEPREG_RTU_ID(struct _srt_ctr *srt);
uint8_t SRT_IM_W_KEEPREG_TRG(struct _srt_ctr *srt);
uint8_t SRT_IM_W_KEEPREG_CYC_TIME(struct _srt_ctr *srt);
uint8_t SRT_IM_W_KEEPREG_TS_TIME(struct _srt_ctr *srt);
uint8_t SRT_IM_W_KEEPREG_POS_SET(struct _srt_ctr *srt);
uint8_t SRT_IM_W_KEEPREG_NEG_SET(struct _srt_ctr *srt);
uint8_t SRT_IM_W_KEEPREG_GEAR(struct _srt_ctr *srt);
uint8_t SRT_IM_W_KEEPREG_IAP_PASS(struct _srt_ctr *srt);
uint8_t SRT_IM_W_KEEPREG_CAIL_ZERO(struct _srt_ctr *srt);

#endif
